package com.ruoyi.store.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.store.dto.StockOutSto;
import com.ruoyi.store.service.IStockOutService;
import com.ruoyi.system.api.domain.caigou.BuyBackOrder;
import com.ruoyi.system.api.domain.store.StockOut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 出库记录Controller
 *
 * @author ruoyi
 * @date 2024-10-31
 */
@RestController
@RequestMapping("/stockOut")
public class StockOutController extends BaseController {
    @Autowired
    private IStockOutService stockOutService;

    /**
     * 查询出库记录列表
     */
    @RequiresPermissions("store:stockOut:list")
    @GetMapping("/list")
    public TableDataInfo list(StockOut stockOut) {
        startPage();
        List<StockOut> list = stockOutService.selectStockOutList(stockOut);
        return getDataTable(list);
    }
    /*获取调拨出库的列表*/
    @RequiresPermissions("store:stockOut:list")
    @GetMapping("/getStockTranList")
    public TableDataInfo getStockTranList(StockOut stockOut) {
        startPage();
        List<StockOut> list = stockOutService.getStockTranList(stockOut);
        return getDataTable(list);
    }

    /**
     * 导出出库记录列表
     */
    @RequiresPermissions("store:stockOut:export")
    @Log(title = "出库记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StockOut stockOut) {
        List<StockOut> list = stockOutService.selectStockOutList(stockOut);
        ExcelUtil<StockOut> util = new ExcelUtil<StockOut>(StockOut.class);
        util.exportExcel(response, list, "出库记录数据");
    }

    /**
     * 获取销售退货可以使用的出库列表
     */
    @RequiresPermissions("store:stockOut:list")
    @GetMapping("/getUseList")
    public TableDataInfo getUseList(StockOut stockOut) {
        startPage();
        List<StockOut> list = stockOutService.getUseList(stockOut);
        return getDataTable(list);
    }

    /**
     * 获取出库记录详细信息
     */
    @RequiresPermissions("store:stockOut:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(stockOutService.selectStockOutById(id));
    }
    /**
     * @return com.ruoyi.common.core.web.page.TableDataInfo
     * @description 付款单获取出库详情
     * @date 2024/11/14 上午8:40
     * @Param stockOut:
     **/
//    @RequiresPermissions("store:stockOut:list")
    @GetMapping("/listForReciveOrder")
    public TableDataInfo listForReciveOrder(StockOut stockOut) {
        startPage();
        List<StockOut> list = stockOutService.listForReciveOrder(stockOut);
        return getDataTable(list);
    }

    /**
     * 新增出库记录
     */
    @RequiresPermissions("store:stockOut:add")
    @GetMapping(value = "/add/{id}")
    public AjaxResult add(@PathVariable("id") int id) {
        return success(stockOutService.addOutStock(id));
    }

    /**
     * @return com.ruoyi.common.core.domain.R<com.ruoyi.system.api.domain.store.StockOut>
     * @description 根据出库单编号获取出库单
     * @date 2024/11/14 上午10:00
     * @Param stockOutCode:
     **/
    @RequiresPermissions("store:stockOut:query")
    @GetMapping("/info/byStockOutCode/{stockOutCode}")
    public R<StockOut> getInfoByStockOutCode(@PathVariable("stockOutCode") String stockOutCode) {
        return R.ok(stockOutService.selectStockOutByStockOutCode(stockOutCode));
    }

    @RequiresPermissions("store:stockOut:addStockOutByTran")
    @GetMapping(value = "/addStockOutByTran/{id}")
    public AjaxResult addStockOutByTran(@PathVariable("id") int id) {
        return success(stockOutService.addStockOutByTran(id));
    }

    @RequiresPermissions("store:stockOut:addStockOutByBuyBack")
    @PostMapping(value = "addStockOutByBuyBack")
    public AjaxResult addStockOutByBuyBack(@RequestBody BuyBackOrder buyBackOrder) {
        return success(stockOutService.addStockOutByBuyBack(buyBackOrder));
    }


    /**
     * 出库
     */
    @RequiresPermissions("store:stockOut:outStock")
    @PostMapping("outStock")
    public AjaxResult outStock(@RequestBody StockOutSto stockOutSto) {
        return AjaxResult.success(stockOutService.outStock(stockOutSto));
    }

    /**
     * 修改出库记录
     */
    @RequiresPermissions("store:stockOut:edit")
    @Log(title = "出库记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockOut stockOut) {
        return toAjax(stockOutService.updateStockOut(stockOut));
    }

    /**
     * 删除出库记录
     */
    @RequiresPermissions("store:stockOut:remove")
    @Log(title = "出库记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(stockOutService.deleteStockOutByIds(ids));
    }
}
